// O(n*log(n)*26)
// run: time -p $exec < input
// opt: 0
#include <iostream>
#include <string>
#include <vector>
#include <map>
#define DEBUG 0

int len[30];
std::vector<int> pos[30];
std::vector<int> tree[30];
int count[30];
std::string s;
int n, odd = -1;

int lowbit(int x) { return x & -x; }

void update(int id, int p, int d)
{
	for (; p <= len[id]; p += lowbit(p))
		tree[id][p] += d;
}

int get_sum(int id, int p)
{
	int ret = 0;
	for (; p > 0; p -= lowbit(p)) ret += tree[id][p];
	return ret;
}

int get(int id, int p)
{
	return pos[id][p] + get_sum(id, p);
}

void interval_update(int id, int l, int r, int d)
{
	if (l > r) return;
	update(id, l, d);
	update(id, r + 1, -d);
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> s;
	n = s.size();
	for (int i = 0; i < n; i++) count[s[i] - 'A']++;
	for (int i = 0; i < 26; i++) {
		if (count[i] & 1) {
			if (odd != -1) { std::cout << "-1\n"; return 0; }
			odd = i;
		}
		pos[i].resize(count[i] + 1);
		tree[i].resize(count[i] + 1);
	}
	for (int i = 0; i < 26; i++) len[i] = count[i];
	for (int i = 0; i < n; i++) {
		int id = s[i] - 'A';
		pos[id][++pos[id][0]] = i;
	}
	for (int i = 0; i < 26; i++) pos[i][0] = 1;

	long long ans = 0, tans;
	for (int i = 0; i < n / 2; i++) {
		int id = 0;
		tans = ans;

#if DEBUG
		int tt = 'I' - 'A';
		std::cerr << "i = " << i << "  " << char('A' + tt) << ": ";
		for (int i = len[tt]; i > 0; i--)
			std::cerr << get(tt, i) << ' ';
		std::cerr << '\n' << pos[tt][0] << ' ' << len[tt] << '\n';
#endif

		for (; id < 26; id++) { if (pos[id][0] > len[id]) continue; if (get(id, pos[id][0]) == i) break; }

#if DEBUG
		std::cerr << char('A' + id) << ' ' << id << '\n';
#endif

		if (id == odd && count[id] == 1) {
			int tid = id;
			for (id = 0; id < 26; id++) {
				if (pos[id][0] > len[id]) continue;
				if (get(id, pos[id][0]) == i + 1) break;
			}
			ans++;
			pos[tid][pos[tid][0]]++;
			pos[id][pos[id][0]]--;
		}

#if DEBUG
		std::cerr << char('A' + id) << ' ' << id << '\n';
#endif

		int last = get(id, len[id]);
		int target = n - 1 - i;
		ans += target - last;

#if DEBUG
		std::cerr << "cost[" << i << "] = " << ans - tans << '\n';
		std::cerr << "target = " << target << "  last = " << last << '\n';
#endif

		len[id]--;
		count[id] -= 2;
		pos[id][0]++;
		for (int j = 0; j < 26; j++) {
			if (pos[j][0] > len[j]) continue;
			int l = pos[j][0], r = len[j];
			while (l + 1 < r) {
				int mid = (l + r) / 2;
				if (get(j, mid) < last) l = mid;
				else r = mid;
			}
//			if (j == 'M' - 'A') std::cerr << " update: " << l << ' ' << r << ' ' << get(j, l) << "\n";
			if (get(j, l) >= last) r = l;
			if (get(j, r) >= last) interval_update(j, r, len[j], -1);
		}
	}
	std::cout << ans << '\n';
}
